﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using Sharp.Infrastructure;

namespace Sharp.Platform.WebAPI.Filter
{
    [DebuggerStepThrough]
    public class HttpHeaderActionFilter : System.Web.Http.Filters.IActionFilter
    {
        public bool AllowMultiple
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
        {
            IEnumerable<string> values;
            if (actionContext.Request.Headers.TryGetValues("currentUser", out values))
            {
                ContactContext.Current.UserId = Guid.Parse(values.ToArray()[0]);
            }


            HttpResponseMessage response = continuation().Result;

            return Task.FromResult(response);
        }
    }
}